package com.ruoyi.system.service.impl.processServiceImpl;


import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.process.ProcessInventory;
import com.ruoyi.system.mapper.process.ProcessInventoryMapper;
import com.ruoyi.system.service.processService.IProcessInventoryService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.xml.transform.Result;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * 加工库存Service业务层处理
 * 
 * @author ljx
 * @date 2020-07-06
 */
@Service
public class ProcessInventoryServiceImpl implements IProcessInventoryService
{
    @Resource
    private ProcessInventoryMapper processInventoryMapper;

    /**
     * 查询加工库存
     * 
     * @param id 加工库存ID
     * @return 加工库存
     */
    @Override
    public ProcessInventory selectProcessInventoryById(Long id)
    {
        return processInventoryMapper.selectProcessInventoryById(id);
    }

    /**
     * 查询加工库存列表
     * 
     * @param processInventory 加工库存
     * @return 加工库存
     */
    @Override
    public List<ProcessInventory> selectProcessInventoryList(ProcessInventory processInventory)
    {
        return processInventoryMapper.selectProcessInventoryList(processInventory);
    }
    
    @Override
    public List<ProcessInventory> selectProcessInventoryListByIds(String ids) {
    	String[] strArray = Convert.toStrArray(ids);
    	List<Long> aList = new ArrayList<Long>();
    	for (String string : strArray) {
    		aList.add(new Long(string));
		}
    	return processInventoryMapper.selectByIds(aList);
    }
    
    @Transactional
    @Override
    public int diviy(List<ProcessInventory> aList) {
    	List<Long> lisyL = new ArrayList<Long>();
    	for (ProcessInventory processInventory : aList) {
    		
    		lisyL.add(processInventory.getId());
    		processInventory.setType(1L);
//    		processInventory.setStatus(5);
    		processInventory.setUpdateTime(new Date());
    		processInventoryMapper.updateProcessInventory(processInventory);
		}
//    	processInventoryMapper.updateStatus(5, lisyL);
    	return 0;
    }

    /**
     * 新增加工库存
     * 
     * @param processInventory 加工库存
     * @return 结果
     */
    @Override
    public AjaxResult insertProcessInventory(ProcessInventory processInventory)
    {
        processInventory.setCreateTime(DateUtils.getNowDate());
        ProcessInventory inventory = new ProcessInventory();
        inventory.setNumber(processInventory.getNumber());
        List<ProcessInventory> list = processInventoryMapper.selectProcessInventoryList(inventory);
        if(CollectionUtils.isNotEmpty(list)){
            return AjaxResult.error("原膜编号重复");
        }
        processInventoryMapper.insertProcessInventory(processInventory);
        return AjaxResult.success();
    }

    /**
     * 修改加工库存
     * 
     * @param processInventory 加工库存
     * @return 结果
     */
    @Override
    public int updateProcessInventory(ProcessInventory processInventory)
    {
        processInventory.setUpdateTime(DateUtils.getNowDate());
        return processInventoryMapper.updateProcessInventory(processInventory);
    }

    /**
     * 删除加工库存对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public AjaxResult deleteProcessInventoryByIds(String ids)
    {
        Boolean flag = false;
        String[] strings = Convert.toStrArray(ids);
        for (String s : strings) {
            Long aLong = Long.valueOf(s);
            ProcessInventory inventory = processInventoryMapper.selectProcessInventoryById(aLong);
            if(inventory==null || inventory.getStatus()!=1){
                flag = true;
                continue;
            }
            processInventoryMapper.deleteProcessInventoryById(aLong);
        }
        return flag?AjaxResult.success("存在已加工原膜不能删除"):AjaxResult.success();
    }

    /**
     * 删除加工库存信息
     * 
     * @param id 加工库存ID
     * @return 结果
     */
    @Override
    public int deleteProcessInventoryById(Long id)
    {
        return processInventoryMapper.deleteProcessInventoryById(id);
    }
}
